package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.mapper.FeedbackMapper;
import com.doubi.model.sys.TAdver;
import com.doubi.model.sys.TFeedback;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import com.doubi.vo.AppUiModel;
import com.doubi.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class FeedbackService extends ServiceImpl<FeedbackMapper, TFeedback> {
    @Autowired
    private FeedbackMapper feedbackMapper;

    public PageVo<TFeedback> getPage(PageVo<TFeedback> pageVo, TFeedback feedback){
        QueryWrapper<TFeedback> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("create_time");
        feedbackMapper.selectPage(pageVo,wrapper);
        return pageVo;
    }
    public AppModel updateHandleBean(TFeedback feedback,String userOid){
        if(DataUtil.isEmpty(feedback.getOid())){
            return AppModel.error();
        }else {
            TFeedback bean = feedbackMapper.selectById(feedback.getOid());
            bean.setHandleRemarks(feedback.getHandleRemarks());
            bean.setHandleOid(userOid);
            bean.setStatus(0);
            bean.setHandleTime(new Date());
            feedbackMapper.updateById(bean);
            return AppModel.success();
        }
    }

    public AppModel getHomeStatistics(){
        Map<String,Integer> map = new HashMap<>();
        QueryWrapper<TFeedback> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        map.put("feedbackCount",feedbackMapper.selectCount(wrapper));
        return AppModel.success(map);
    }
    /**
     * MrDoubi
     * 2020年3月16日 13:19:01
     * 意见反馈
     * @param content 内容
     * @param imgUrls 图片
     * @param contact 联系方式
     * @param userOid 用户唯一标识
     * @return model
     */
    public AppUiModel<String> saveFeedback(String content, String imgUrls, String contact, String userOid) {
        if (DataUtil.isEmpty(content)) {
            return new AppUiModel<>(400,"请填写意见!");
        }
        if (DataUtil.isEmpty(contact)) {
            return new AppUiModel<>(400,"请填写联系方式!");
        }
        TFeedback feedback = new TFeedback();
        feedback.setContact(content);
        feedback.setImgUrls(imgUrls);
        feedback.setContact(contact);
        feedback.setUserOid(userOid);
        feedback.setCreateTime(new Date());
        feedback.setStatus(1);
        feedbackMapper.insert(feedback);
        return new AppUiModel<>(200,"感谢你的反馈!");
    }
}
